unit uFrmPesquisaPadrao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, ComCtrls, uDM1, FMTBcd,
  Provider, DBClient, DB, SqlExpr , Contnrs, Mask;

type
  TFrmPesquisaPadrao = class(TForm)
    StatusBar1: TStatusBar;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    BitBtn1: TBitBtn;
    dsPesquisa: TDataSource;
    sdsPesquisa: TSQLDataSet;
    cdsPesquisa: TClientDataSet;
    dspPesquisa: TDataSetProvider;
    Button1: TButton;
    MaskEdit1: TMaskEdit;
    procedure BitBtn1Click(Sender: TObject);
    procedure MaskEdit1KeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    Resultado : Variant;
    { Public declarations }
  end;

  TPesquisa = class

  private
    FCodRetorno: variant;
    FCampoRetorno: string;
    FTabela: string;
    FSQL: string;
    FCampos: TObjectList;
    FTabelaApelido: string;
    procedure SetCodRetorno(const Value: variant);
    procedure SetCampoRetorno(const Value: string);
    procedure SetTabela(const Value: string);
    procedure SetSQL(const Value: string);
    procedure SetCampos(const Value: TObjectList);
    procedure SetTabelaApelido(const Value: string);

  public
    property CodRetorno : variant read FCodRetorno write SetCodRetorno;
    property CampoRetorno : string read FCampoRetorno write SetCampoRetorno;
    property Tabela : string read FTabela write SetTabela;
    property SQL : string read FSQL write SetSQL;
    procedure GerarSQL;
    function CarregaDadosDadosTabela : boolean;
    property Campos : TObjectList read FCampos write SetCampos;
    procedure PopularComboBox ( CBB : TComboBox ; Item : string);
    procedure Pesquisar;
    property TabelaApelido : string read FTabelaApelido write SetTabelaApelido;

//  end;
  published
    destructor Destroy;override;
  end;

  TDefCampo = class
    private
    FDbGrid: boolean;
    FComboBox: boolean;
    FNome: string;
    FTipo: string;
    FLabelCampo: string;
    FTamanho: integer;
    FApelidoTabela: string;
    procedure SetComboBox(const Value: boolean);
    procedure SetDbGrid(const Value: boolean);
    procedure SetLabelCampo(const Value: string);
    procedure SetNome(const Value: string);
    procedure SetTamanho(const Value: integer);
    procedure SetTipo(const Value: string);
    procedure SetApelidoTabela(const Value: string);

    public
      property Nome : string read FNome write SetNome;
      property LabelCampo : string read FLabelCampo write SetLabelCampo;
      property Tipo : string read FTipo write SetTipo;
      property Tamanho : integer read FTamanho write SetTamanho;
      property DbGrid : boolean read FDbGrid write SetDbGrid;
      property ComboBox : boolean read FComboBox write SetComboBox;
      property ApelidoTabela : string read FApelidoTabela write SetApelidoTabela;
  end;



var
  FrmPesquisaPadrao: TFrmPesquisaPadrao;
  Pesquisa : TPesquisa;

implementation

{$R *.dfm}

{ TPesquisa }

function TPesquisa.CarregaDadosDadosTabela: boolean;
var Campo : TDefCampo;
  sdsTemp : TSQLDataSet;
  i : integer;
begin
  Campos := TObjectList.Create;
  sdsTemp := TSQLDataSet.Create(nil);
  try
    try
      sdsTemp.SQLConnection := DM1.CONEXAO;
      sdsTemp.CommandText := 'select * from campo_pesquisa where tabela_pesquisa = ' + QuotedStr(Self.Tabela);
      sdsTemp.Open;
      while not sdsTemp.Eof do
      begin
        campo := TDefCampo.Create;
        campo.Nome := sdsTemp.FieldByName('CAMPO_PESQUISA').AsString;
        Campo.Tipo := sdsTemp.FieldByName('TIPO_CAMPO_PESQUISA').AsString;
        Campo.Tamanho := sdsTemp.FieldByName('TAMANHO_CAMPO_PESQUISA').AsInteger;
        Campo.LabelCampo := sdsTemp.FieldByName('LABEL_CAMPO_PESQUISA').AsString;
        Campo.DbGrid := sdsTemp.FieldByName('DBGRID_PESQUISA').AsBoolean;
        CAMPO.ComboBox := sdsTemp.FieldByName('COMBOBOX_PESQUISA').AsBoolean;
        Campo.ApelidoTabela := sdsTemp.FieldByName('APELIDO_TABELA_PESQUISA').AsString;
        Campos.Add(Campo);
        sdsTemp.Next
      end;
      for i := 0 to Campos.Count - 1 do
        begin
          if TDefCampo(campos.Items[i]).ComboBox = True then
            FrmPesquisaPadrao.ComboBox1.AddItem(TDefCampo(Campos.Items[i]).LabelCampo,TDefCampo(Campos.Items[i]));
        end;
       FrmPesquisaPadrao.ComboBox1.ItemIndex := 0;
    finally

    end;

  finally
    sdsTemp.Free;
  end;




end;




destructor TPesquisa.Destroy;
begin
  Self.Campos.Free;
  inherited;
end;

procedure TPesquisa.GerarSQL;
var StringListAux : TStringList;
  i : integer;
  condicao , conteudo , campo : string;

begin
  campo := TDefCampo(FrmPesquisaPadrao.ComboBox1.Items.Objects[FrmPesquisaPadrao.ComboBox1.ItemIndex]).ApelidoTabela + '.' + TDefCampo(FrmPesquisaPadrao.ComboBox1.Items.Objects[FrmPesquisaPadrao.ComboBox1.ItemIndex]).Nome;
  if FrmPesquisaPadrao.ComboBox2.ItemIndex = 5 then
  begin
    condicao := 'like';
    conteudo := QuotedStr('%' + FrmPesquisaPadrao.MaskEdit1.Text + '%');
  end
  else
  if FrmPesquisaPadrao.ComboBox2.ItemIndex = 6 then
  begin
    condicao := 'like';
    conteudo := QuotedStr(FrmPesquisaPadrao.MaskEdit1.Text + '%');
  end
  else
  begin
    condicao := FrmPesquisaPadrao.ComboBox2.Text;
    conteudo := QuotedStr(FrmPesquisaPadrao.MaskEdit1.Text);
  end;


  StringListAux := TStringList.Create;
  StringListAux.StrictDelimiter := true;
  StringListAux.Delimiter := ',';
  for i := 0 to Self.Campos.Count - 1 do
  with StringListAux do
  begin
    Add(TDefCampo(Self.Campos[i]).ApelidoTabela + '.' + TDefCampo(Self.Campos[i]).Nome);
  end;
  Self.SQL := 'select ' + StringListAux.DelimitedText + ' from ' + Self.Tabela + ' ' + Self.TabelaApelido;
  ShowMessage(Self.SQL);
  StringListAux.Free;
end;

procedure TPesquisa.Pesquisar;
begin
  if TDefCampo(FrmPesquisaPadrao.ComboBox1.Items.Objects[(FrmPesquisaPadrao.ComboBox1.ItemIndex)]).Tipo = 'STRING' then


end;

procedure TPesquisa.PopularComboBox(CBB: TComboBox ; Item : string);
begin
  CBB.Items.Add(Item);
end;

procedure TPesquisa.SetCampoRetorno(const Value: string);
begin
  FCampoRetorno := Value;
end;

procedure TPesquisa.SetCampos(const Value: TObjectList);
begin
  FCampos := Value;
end;

procedure TPesquisa.SetCodRetorno(const Value: variant);
begin
  FCodRetorno := Value;
end;

procedure TPesquisa.SetSQL(const Value: string);
begin
  FSQL := Value;
end;

procedure TPesquisa.SetTabela(const Value: string);
begin
  FTabela := Value;
end;

procedure TPesquisa.SetTabelaApelido(const Value: string);
begin
  FTabelaApelido := Value;
end;

{ TDefCampo }

procedure TDefCampo.SetApelidoTabela(const Value: string);
begin
  FApelidoTabela := Value;
end;

procedure TDefCampo.SetComboBox(const Value: boolean);
begin
  FComboBox := Value;
end;

procedure TDefCampo.SetDbGrid(const Value: boolean);
begin
  FDbGrid := Value;
end;

procedure TDefCampo.SetLabelCampo(const Value: string);
begin
  FLabelCampo := Value;
end;

procedure TDefCampo.SetNome(const Value: string);
begin
  FNome := Value;
end;

procedure TDefCampo.SetTamanho(const Value: integer);
begin
  FTamanho := Value;
end;

procedure TDefCampo.SetTipo(const Value: string);
begin
  FTipo := Value;
end;

procedure TFrmPesquisaPadrao.BitBtn1Click(Sender: TObject);
//var Pesquisa : TPesquisa;
begin
  Pesquisa := TPesquisa.Create;
  Pesquisa.Tabela := 'CONSULTOR';
  Pesquisa.TabelaApelido := 'CON';
  Pesquisa.CarregaDadosDadosTabela;
  Pesquisa.GerarSQL;
  sdsPesquisa.CommandText := Pesquisa.SQL;
  sdsPesquisa.Open;
  cdsPesquisa.Open;
end;

procedure TFrmPesquisaPadrao.Button1Click(Sender: TObject);
begin
  //ShowMessage(FrmPesquisaPadrao.ComboBox2.Text);
  //ShowMessage(TDefCampo(FrmPesquisaPadrao.ComboBox1.Items.Objects[FrmPesquisaPadrao.ComboBox1.ItemIndex]).ApelidoTabela + '.' + TDefCampo(FrmPesquisaPadrao.ComboBox1.Items.Objects[FrmPesquisaPadrao.ComboBox1.ItemIndex]).Nome);;

end;

procedure TFrmPesquisaPadrao.FormDestroy(Sender: TObject);
begin
  Pesquisa.Free;


end;

procedure TFrmPesquisaPadrao.MaskEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  if TDefCampo(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).Tipo = 'INTEGER' then
  begin
    if not (Key in ['0'..'9']) then
      Key := #0;
  end;

end;

end.
